package com.collabscm.pf.validation;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import com.collabscm.pf.AbstractTestCase;

@ContextConfiguration(locations={"classpath:config/application-config.xml"})
public class TestBeanValidationUtils extends AbstractTestCase {
	/**
	 * 待测试的Bean
	 */
	public static class TestBean {
		@NotNull
		private String userCode;
		@NotNull
		private String userName;
		
		public String getUserCode() {
			return userCode;
		}
		public void setUserCode(String userCode) {
			this.userCode = userCode;
		}
		public String getUserName() {
			return userName;
		}
		public void setUserName(String userName) {
			this.userName = userName;
		}
	}
	
	public static class AObject {
		@NotNull
		@Valid
		private TestBean bean;

		public TestBean getBean() {
			return bean;
		}

		public void setBean(TestBean bean) {
			this.bean = bean;
		}
		
	}
	
	@Test
	public void testValidate() {
		TestBean tb = new TestBean();
		AObject a = new AObject();
		a.setBean(tb);
		Set<ConstraintViolation<AObject>> contraints = BeanValidationUtils.validate(false, a);
		logger.debug("验证结果为：");
		for(ConstraintViolation<AObject> c: contraints) {
			logger.debug("验证错误：{}:{}", c.getPropertyPath(), c.getMessage());
		}
		Set<ConstraintViolation<TestBean>> tbContraints = BeanValidationUtils.validate(false, tb);
		logger.debug("验证结果为：");
		for(ConstraintViolation<TestBean> c: tbContraints) {
			logger.debug("验证错误：{}:{}", c.getPropertyPath(), c.getMessage());
		}
	}

}
